#include "stdafx.h"
#include "main.h"
#include <QCmdLine.h>

#if !defined(QTOOLS_GUI)
int main(int argc, char *argv[])
{
	std::string args;
	if( argc == 1 ){
		std::cout << usage();
#ifndef NDEBUG
		system("PAUSE");
#endif
		return -1;
	}
	for( int i=1; i<argc; i++ )
		args += std::string(argv[i]) + " ";

	int ret = treatCommands( trim(args) );
	if(verbose)std::cout << "Command returned: " << ret << std::endl;
	

	if( ret == CMD_FAILED || (ret & CMD_FAILED) != NULL )
	{
#ifndef NDEBUG
		system("PAUSE");
#endif
		return -1;
	}
	else if( ret == CMD_SUCCESS ){
		try
		{
			init();
		}
		catch( std::exception e )
		{
			std::cerr << "Couldn't create snapshot because " << e.what() << std::endl;
			return CMD_FAILED;
		}
		catch( ... )
		{
			std::cerr << "Couldn't create snapshot because an unknown error happened !" << std::endl;
			return CMD_FAILED;
		}
	}
#ifndef NDEBUG
	system("PAUSE");
#else
	if( verbose )
		system("PAUSE");
#endif
	return ret;
}
#else
#ifndef NDEBUG
	#pragma comment(lib,"qtmaind.lib")
	#pragma comment(lib,"Qt5Cored.lib")
	#pragma comment(lib,"Qt5Guid.lib")
	#pragma comment(lib,"Qt5Widgetsd.lib")
#else
	#pragma comment(lib,"qtmain.lib")
	#pragma comment(lib,"Qt5Core.lib")
	#pragma comment(lib,"Qt5Gui.lib")
	#pragma comment(lib,"Qt5Widgets.lib")
#endif

	#include <qapplication.h>
	#include "gui.h"
	#include "LitFactory.h"

	class TestItem : public Lit::FactoryItem<TestItem>
	{
		friend class TestFactory;
	public:
		TestItem( const std::string &name, Lit::Factory<TestItem> * creator ):FactoryItem( name, creator ){}
		~TestItem(){}
	};

	typedef Lit::FactoryItemPtr<TestItem> TestItemPtr;
	
	class TestFactory : public Lit::Factory<TestItem>
	{
	protected:
		TestItem* createImpl( const std::string &name ){
			return new TestItem( name, this );
		}
		void destroyImpl( TestItem* itm ){
			if( itm ) delete itm, itm = NULL;
		}
	public:
		TestFactory() : Factory( "TestFactory" ){}
		~TestFactory(){ destroyResources(); }
	};
	int main(int argc, char *argv[])
	{
		{
			TestFactory * test = new TestFactory( );
			TestItemPtr itm(test->createResource( "test" ));
			delete test, test = NULL;
		}

		QApplication app(argc, argv);
		try
		{
			homePath = std::string(getenv("LIT_HOME"));
			QCmdLine::Instance( argc, argv );
			if( homePath.empty() ) return QDialog::Rejected;
			SnapshotGui gui;
			gui.setWindowIcon( QIcon(":/Tools/Icons/add_archive.png") );
			gui.setIconSize( QSize( 64, 64 ) );
		
			gui.show();
			return app.exec();
		}
		catch( std::exception ex )
		{
			QMessageBox::critical( NULL, "Critical Error", ex.what() );
		}
		catch( ... )
		{
			QMessageBox::critical( NULL, "Critical Error", "Unknown Error" );
		}
		return -1;
	}

#endif